package com.yearner.reviewplan.user.model;

import com.google.gson.Gson;
import com.yearner.reviewplan.common.model.BaseRepository;
import com.yearner.reviewplan.common.util.MyJdbcUtil;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import static com.yearner.reviewplan.user.enums.UserTableEnum.*;
/**
 * @author pengyg
 * @since 2019/12/5
 */
@Repository
public class UserBaseRepository extends BaseRepository {
    protected final Gson gson;

    public UserBaseRepository(MyJdbcUtil myJdbcUtil,Gson gson) {
        super(myJdbcUtil);
        this.gson = gson;
    }


    @Override
    protected RowMapper<User> mapper() {
        return (rs, rowNum) -> {
            UserId userId = UserId.byId(rs.getString(ID.getColumn()));
            Cycle cycle = gson.fromJson(rs.getString(CYCLE.getColumn()), Cycle.class);

            return UserFactory.create
                    (userId,
                    rs.getString(ACCOUNT.getColumn()),
                    rs.getString(PASSWORD.getColumn()),
                    cycle);
        };
    }
}
